<?php
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class EcommerceModelCompanies extends JModel
{
	var $_data = null;
	var $_table = '#__pr_company';
	var $_list = null; 
	var $_page = null; 

	function &getTable()
	{
		if ($this->_table == null) {
			$this->_table = JTable::getInstance('companies', $this->getDBO() );
		}
		return $this->_table;
	}

	function getList()
	{
		global $mainframe;

		if (!empty($this->_list)) {
			return $this->_list;
		}

		// Initialize variables
		$db		=& $this->getDBO();
		
		$search = null;
		$where = array();
		$search				= $mainframe->getUserStateFromRequest('companies.search', 'search', '',	'string');
		$search				= JString::strtolower($search);
		
		if ($search) {
			$where[] = 'LOWER( name ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false ) . ' OR LOWER( name_en ) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
		}
		
		$where = (count($where) ? ' WHERE '.implode(' AND ', $where) : '');

		// Get some variables from the request
		$option				= JRequest::getCmd( 'option' );
		$limit				= $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest('companies.limitstart', 'limitstart',		0,	'int');


		// Get the total number of records
		$query = 'SELECT COUNT(*) FROM #__pr_company ' . $where;
		$db->setQuery($query);
		$total = $db->loadResult();

		// Create the pagination object
		jimport('joomla.html.pagination');
		$this->_page = new JPagination($total, $limitstart, $limit);

		// Get the articles
		$query = 'SELECT * ' .
				' FROM #__pr_company ' . $where .
				' GROUP BY id ORDER BY ordering';
		$db->setQuery($query, $this->_page->limitstart, $this->_page->limit);
		$this->_list = $db->loadObjectList();

		//If there is a db query error, throw a HTTP 500 and exit
		if ($db->getErrorNum()) {
			JError::raiseError( 500, $db->stderr() );
			return false;
		}

		return $this->_list;
	}

	function getPagination()
	{
		if (is_null($this->_list) || is_null($this->_page)) {
			$this->getList();
		}
		return $this->_page;
	}
	
	

}

?>
